package sw6.pc.network;

import java.io.*;

public class Serializer {
	public static byte[] serializeObject(Object object) {

	     ByteArrayOutputStream baos  = new ByteArrayOutputStream();
	     ObjectOutputStream oos   = null;
	     try {
	   oos = new ObjectOutputStream(baos);
	   oos.writeObject(new STest("Steffan"));
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	  
	  byte[] byteArrayOutputSerialized = baos.toByteArray();
	  
	  return byteArrayOutputSerialized;
	}
	
	public static <T> T deSerializeObject(byte[] bytes, Class<T> someClass ) {

		  ByteArrayInputStream bais   = new ByteArrayInputStream(bytes);
		  ModifiedObjectInputStream ois    = null;
		  
		  Object obj = null;
		  try {
		   ois = new ModifiedObjectInputStream(bais, someClass);
		   obj = ois.readObject();
		   ois.close();
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		  return someClass.cast(obj);
	}
}